﻿/**
 * 实现一些公用的数据库查询业务
 * 1.部门列表查询
 * 2.职位列表查询
 */
var Promise = require('bluebird');
var mongoose = require('mongoose');
var dptInfoMdl = require('../module/DptInfo');
var positionInfoMdl = require('../module/PositionInfo');

'use strict'

var exportCtller = {};

var CreateOrUpdatePromise = function (mdl, condtion, newdata) {
    return new Promise(function (fulfill, reject) {
        if (!condtion) {
            mdl.create(newdata, function (err, doc) {
                if (err) { reject(err); }
                else { fulfill(doc); }
            });
        } else {
            mdl.update(condtion, newdata, function (err, doc) {
                if (err) { reject(err); }
                else { fulfill(doc); }
            });
        }
    });
}

var delPromise = function (mdl, condition) {
    return new Promise(function (fulfill, reject) {
        if (condition) {
            mdl.remove(condition, function (err) {
                if (err) { reject(err); }
                else { fulfill(null); }
            });
        }
    });
}

exportCtller.dbQuery = function (queryID, callback) {
    switch (queryID) {
        case "#dpt":
            dptInfoMdl.find({}, null, { sort: { dpt_name: 1 }}, function (err, docs) {
                var dptInfo = [];
                if (docs instanceof Array) {
                    docs.forEach(function (v, i) {
                        dptInfo.push({ dpt_name: v.dpt_name, id: v._id });
                    });
                }
                callback(err, dptInfo);
            });
            break;
        case "#position":
            positionInfoMdl.find({}, null, { sort: { position: 1} }, function (err, docs) {
                var positionInfo = [];
                if (docs instanceof Array) {
                    docs.forEach(function (v, i) {
                        positionInfo.push({ position: v.position, id: v._id });
                    });
                }
                callback(err, positionInfo);
            });
            break;
    }
};

exportCtller.dbBaseInfoUpd = function (infoId, data, toDel, callback) {
    var mdl,
        col_name,
        promises = [];
    switch (infoId) {
        case "#dpt":
            mdl = dptInfoMdl;
            col_name = "dpt_name";
            break;
        case "#position":
            mdl = positionInfoMdl;
            col_name = "position";
            break;
    };

    data.forEach(function (row, i) {
        var newDoc = {},
            condition;
        newDoc[col_name] = row[1];
        if (typeof row[0] == "string") {
            if ((row[0] || "") == "") {
                condition = null;
            } else {
                condition = { _id: mongoose.Types.ObjectId(row[0]) };
            }
        }
        else {
            condition = { _id: row[0] };
        }
        promises.push(CreateOrUpdatePromise(mdl, condition, newDoc));
    });

    toDel.forEach(function (row, i) {
        var condition;
        if (typeof row[0] == "string") {
            if ((row[0] || "") == "") {
                condition = null;
            } else {
                condition = { _id: mongoose.Types.ObjectId(row[0]) };
            }
        }
        else {
            condition = { _id: row[0] };
        }
        promises.push(delPromise(mdl, condition));
    });

    Promise.all(promises)
        .then(function (values) {
            console.log("mongodb update finish:", JSON.stringify(values));
            callback(null, values);
        })
        .catch(function (reason) {
            console.log("mongodb update err:", reason);
            callback(reason, null);
        });
};

module.exports = exportCtller;